function fnames = bvqxfile_requestfile(nrf, pattern, ext, formats, saveas)
% BVQXfile::<PRIVATE>::requestfile  - request a file interactively
%
% THIS FUNCTION IS AN INTERNAL FUNCTION OF THE CLASS
%
% @BVQXfile
%
% AND IT SHOULD NEVER BE CALLED FROM OUTSIDE THE CLASS CODE

% Version:  v0.7b
% Build:    7082923
% Date:     Aug-29 2007, 11:18 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% default: no files
fnames = {};

% get or save
if nargin < 5 || ...
    isempty(saveas) || ...
   (~islogical(saveas) && ...
    ~ischar(saveas))
    saveas = false;
end

% request type and/or multiple files
if islogical(saveas) && ...
    saveas(1)
    mf = 'off';
    if ischar(nrf)
        dtitle = nrf(:)';
    else
        dtitle = 'Please select or type a filename for saving...';
    end
elseif ischar(saveas)
    mf = 'off';
    dtitle = saveas(:)';
    saveas = false;
elseif nrf > 1
    mf = 'on';
    dtitle = 'Please choose at least one file...';
else
    mf = 'off';
    dtitle = 'Please choose a file...';
end

% parse pattern
[pp, pf, pe] = fileparts(pattern);
if ~isempty(pp)
    op = cd;
    try
        cd(pp);
    catch
        pp = '';
    end
end
if ~isempty(pe)
    pe(1) = [];
end
if isempty(pe)
    pe = '*';
end

% build pattern list
pattlist = cell(0, 2);

% get valid extensions
exl = fieldnames(ext);
exf = find(strcmpi(exl, pe));
if ~isempty(exf)
    fmt = ext.(exl{exf(1)});
    spec = formats.(lower(fmt{1}(end-2:end)))(fmt{2});
    exf = find(strcmpi(spec.Extensions, pe));
    desc = spec.Description{exf(1)};
    if isempty(strfind(desc, '*.'))
        pattlist(end + 1, :) = {['*.' pe], [desc ' (*.' pe ')']};
    else
        [exm{1:3}] = regexp(desc, '\*\.([a-zA-Z0-9_]+)');
        if ~isempty(exm{3})
            newx = desc(exm{3}{1}(1, 1):exm{3}{1}(1, 2));
            pattlist(end + 1, :) = {['*.' newx], desc};
        end
    end
end

% add all types to pattern list for defined types
if ~strcmp(pe, '*')
    pattlist(end + 1, :) = {'*.*', 'All files (*.*)'};
    
% otherwise create list !
else
    ftl = { ...
        'fmr', 'amr', 'vmr', 'v16', ...
        'vtc', 'glm', 'vmp', ...
        'srf', 'mtc', 'smp'};
    for tc = 1:numel(ftl)
        fmt = ext.(ftl{tc});
        spec = formats.(lower(fmt{1}(end-2:end)))(fmt{2});
        exf = find(strcmpi(spec.Extensions, ftl{tc}));
        desc = spec.Description{exf(1)};
        if isempty(strfind(desc, '*.'))
            pattlist(end + 1, :) = {['*.' ftl{tc}], [desc ' (*.' ftl{tc} ')']};
        else
            [exm{1:3}] = regexp(desc, '\*\.([a-zA-Z0-9_]+)');
            if ~isempty(exm{3})
                newx = desc(exm{3}{1}(1, 1):exm{3}{1}(1, 2));
                pattlist(end + 1, :) = {['*.' newx], desc};
            end
        end
    end
    pattlist(end + 1, :) = {'*.*', 'All files (*.*)'};
end

% check for filename
if strcmp(pf, '*')
    pf = cell(0, 1);
else
    pf = {[pf '.' pe]};
end

% try getting file
try
    if ~saveas
        if mainver > 6
            [fnames, fpath] = uigetfile(pattlist, dtitle, pf{:}, 'MultiSelect', mf);
        else
            [fnames, fpath] = uigetfile(pattlist, dtitle, pf{:});
        end
    else
        [fnames, fpath] = uiputfile(pattlist, dtitle, pf{:});
    end
catch
    if ~isempty(pp)
        try
            cd(op);
        catch
            % do nothing
        end
    end
    warning( ...
        'BVQXfile:CallError', ...
        'Error calling uigetfile/uiputfile(...): %s.', ...
        lasterr ...
    );
    return;
end
if ~isempty(pp)
    try
        cd(op);
    catch
        % do nothing
    end
end

% return on cancel
if isequal(fnames, 0) || ...
    isequal(fpath, 0)
    fnames = {};
    return;
end

% make cell array
if ~iscell(fnames)
    fnames = {fnames};
end

% prepend path
fpath = strrep(fpath, filesep, '/');
if ~isempty(fpath) && ...
    fpath(end) ~= '/'
    fpath(end + 1) = '/';
end
for fc = 1:numel(fnames)
    fnames{fc} = [fpath fnames{fc}];
end
